﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
namespace BLL.Admin
{
    public class TinhHelper
    {
        public static void Add_Tinh(int QuocGiaId, string Ten)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_DM_Tinh tinh = new tbl_DM_Tinh();
            tinh.QuocGiaId = QuocGiaId;
            tinh.Ten = Ten;
            db.tbl_DM_Tinhs.InsertOnSubmit(tinh);
            db.SubmitChanges();
        }
        public static void Update_Tinh(int TinhId,int QuocGiaId, string Ten)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_DM_Tinh tinh = db.tbl_DM_Tinhs.FirstOrDefault(k=>k.TinhId==TinhId);
            tinh.QuocGiaId = QuocGiaId;
            tinh.Ten = Ten;            
            db.SubmitChanges();
        }
        public static tbl_DM_Tinh Get_TinhId(int TinhId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            tbl_DM_Tinh tinh = db.tbl_DM_Tinhs.FirstOrDefault(k => k.TinhId == TinhId);
            return tinh;
        }
        public static IQueryable List_Tinh_Search(int QuocGiaId, string Ten)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tinh in db.tbl_DM_Tinhs
                        join quocgia in db.tbl_DM_QuocGias on tinh.QuocGiaId equals quocgia.QuocGiaId
                        where tinh.Ten.Contains(Ten) && (QuocGiaId == 0 || tinh.QuocGiaId == QuocGiaId)
                        orderby tinh.Ten ascending
                        select new { 
                            tinh.TinhId,
                            tinh.Ten,
                            QuocGia=quocgia.Ten,                            
                        };
            return query;
        }
        public static IQueryable List_Tinh_Theo_QuocGia(int QuocGiaId)
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tinh in db.tbl_DM_Tinhs
                        join quocgia in db.tbl_DM_QuocGias on tinh.QuocGiaId equals quocgia.QuocGiaId
                        where quocgia.QuocGiaId ==QuocGiaId
                        orderby tinh.Ten ascending
                        select new
                        {
                            tinh.TinhId,
                            tinh.Ten,                            
                        };
            return query;
        }
        public static IQueryable List_ALL_Tinh()
        {
            var db = Connect.GetDataContext<TimViecDataContext>("Strconnect");
            var query = from tinh in db.tbl_DM_Tinhs
                        join quocgia in db.tbl_DM_QuocGias on tinh.QuocGiaId equals quocgia.QuocGiaId
                        orderby tinh.Ten ascending
                        select new
                        {
                            tinh.TinhId,
                            tinh.Ten,
                        };
            return query;
        }
    }
}